package com.ruoyi.business.controller;

import com.ruoyi.business.domain.BuyerInfo;
import com.ruoyi.business.domain.OrderInfo;
import com.ruoyi.business.service.BuyerInfoService;
import com.ruoyi.business.service.IOrderInfoService;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 订单管理Controller
 * 
 * @author ruoyi
 * @date 2025-07-11
 */
@RestController
@RequestMapping("/business/orderInfo")
public class OrderInfoController extends BaseController {
    @Autowired
    private IOrderInfoService orderInfoService;
    @Autowired
    private BuyerInfoService buyerInfoService;

    /**
     * 查询订单管理列表
     */
    @PreAuthorize("@ss.hasPermi('business:orderInfo:list')")
    @GetMapping("/list")
    public TableDataInfo list(OrderInfo orderInfo) {
        startPage();
        List<OrderInfo> list = orderInfoService.selectOrderInfoList(orderInfo);
        list.forEach(item->{
            BuyerInfo buyerInfo = buyerInfoService.selectBuyerInfoByWid(item.getWid());
            item.setRemark6(buyerInfo.getName());
        });
        return getDataTable(list);
    }

    /**
     * 导出订单管理列表
     */
    @PreAuthorize("@ss.hasPermi('business:orderInfo:export')")
    @Log(title = "订单管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, OrderInfo orderInfo) {
        List<OrderInfo> list = orderInfoService.selectOrderInfoList(orderInfo);
        ExcelUtil<OrderInfo> util = new ExcelUtil<OrderInfo>(OrderInfo.class);
        util.exportExcel(response, list, "订单管理数据");
    }

    /**
     * 获取订单管理详细信息
     */
    @PreAuthorize("@ss.hasPermi('business:orderInfo:query')")
    @GetMapping(value = "/{outerOrderNo}")
    public AjaxResult getInfo(@PathVariable("outerOrderNo") String outerOrderNo) {
        return success(orderInfoService.selectOrderInfoByOuterOrderNo(outerOrderNo));
    }

    /**
     * 新增订单管理
     */
    @PreAuthorize("@ss.hasPermi('business:orderInfo:add')")
    @Log(title = "订单管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody OrderInfo orderInfo) {
        return toAjax(orderInfoService.insertOrderInfo(orderInfo));
    }

    /**
     * 修改订单管理
     */
    @PreAuthorize("@ss.hasPermi('business:orderInfo:edit')")
    @Log(title = "订单管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody OrderInfo orderInfo) {
        return toAjax(orderInfoService.updateOrderInfo(orderInfo));
    }

    /**
     * 删除订单管理
     */
    @PreAuthorize("@ss.hasPermi('business:orderInfo:remove')")
    @Log(title = "订单管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{outerOrderNos}")
    public AjaxResult remove(@PathVariable String[] outerOrderNos) {
        return toAjax(orderInfoService.deleteOrderInfoByOuterOrderNos(outerOrderNos));
    }

    /**
     * 批量复制订单
     */
    @PreAuthorize("@ss.hasPermi('business:orderInfo:add')")
    @Log(title = "订单管理", businessType = BusinessType.INSERT)
    @PostMapping("/batchCopy")
    public AjaxResult batchCopy(@RequestBody List<String> outerOrderNos) {
        return toAjax(orderInfoService.batchCopyOrderInfo(outerOrderNos));
    }

    /**
     * 推送订单到微盟 :发送
     * @param outerOrderNos 外部订单编号
     * @return
     */
    @PreAuthorize("@ss.hasPermi('business:orderInfo:send')")
    @PostMapping("/sendOrderInfo")
    public AjaxResult sendOrderInfo(@RequestBody List<String> outerOrderNos) {
        orderInfoService.sendOrderInfo(outerOrderNos);
        return AjaxResult.success("订单推送完成");
    }
    /**
     * 推送物流到微盟：发货
     * @param outerOrderNos 外部订单编号
     * @return
     */
    @PreAuthorize("@ss.hasPermi('business:orderInfo:logistics')")
    @PostMapping("/sendLogistics")
    public AjaxResult sendLogistics(@RequestBody List<String> outerOrderNos) {
        orderInfoService.sendLogistics(outerOrderNos);
        return AjaxResult.success("订单发货成功！");
    }

    /**
     * 推送状态到微盟：完成 & 调整积分
     * @param outerOrderNos 外部订单编号
     * @return
     */
    @PreAuthorize("@ss.hasPermi('business:orderInfo:finish')")
    @PostMapping("/sendFinishStatus")
    public AjaxResult sendFinishStatus(@RequestBody List<String> outerOrderNos) {
        orderInfoService.sendFinishStatus(outerOrderNos);
        orderInfoService.adjustPoint(outerOrderNos);
        return AjaxResult.success("订单发货成功！");
    }

    /**
     * 推送状态到微盟：取消
     * @param outerOrderNos 外部订单编号
     * @return
     */
    @PreAuthorize("@ss.hasPermi('business:orderInfo:sendCancelStatus')")
    @PostMapping("/sendCancelStatus")
    public AjaxResult sendCancelStatus(@RequestBody List<String> outerOrderNos) {
        orderInfoService.sendCancelStatus(outerOrderNos);
        return AjaxResult.success("订单已取消！");
    }

    /**
     * 订单恢复
     * @param outerOrderNos
     * @return
     */
    @PreAuthorize("@ss.hasPermi('business:orderInfo:restoreStatus')")
    @PostMapping("/restoreStatus")
    public AjaxResult restoreStatus(@RequestBody List<String> outerOrderNos) {
        orderInfoService.restoreStatus(outerOrderNos);
        return AjaxResult.success("订单恢复成功！");
    }
}
